<?php
/*
 * Created on 20-Jun-2006
 *
 * To change the template for this generated file go to
 * Window - Preferences - PHPeclipse - PHP - Code Templates
 *
 *
 * @copyright &copy; 2006 The Open University
 * @author a.p.carter@open.ac.uk
 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
 * @package package_name
 *
 */





/**
 * Sends an HTTP redirect in the header. Can accept a relative path.
 *
 * This is useful only if the page does not intend to print out any
 * other content. You can't combine it with displaying content.
 *
 * The relative URL can include ../. For example, if the script that
 * was called is moodle/mod/tadpole/blub.php and, '../test.php' would
 * refer to moodle/mod/test.php, while plain 'squelch.php' would refer to
 * moodle/mod/tadpole/squelch.php.
 *
 * @param string $url URL to redirect to (absolute or relative to 
 *   currently-running script)
 */
function get_url($url) {
    // Leave absolute URLs alone
    if(!preg_match('/^[a-z]+:/',$url)) { // Relative URL
        global $CFG;
        // Add in the host etc. (from wwwroot) to the current folder
        // from SCRIPT_NAME, then add the new target onto the end.
        $url=preg_replace('/^(.*?[^:\/])\/.*$/','$1',$CFG->wwwroot).
            rtrim(dirname($_SERVER['SCRIPT_NAME']),'/\\').'/'.$url;
        // Replace all ..s
        while(true) {
            $newurl=preg_replace('/\/[^\/]*\/\.\.\//','/',$url);
            if($newurl==$url) {
                break;
            }
            $url=$newurl;
        }
    }
    return($url);
}

 

 

    // displays a page showing resourcepages that can be shared

    require_once("module_resourcepage.php");
    $module=module_resourcepage::$inst;
 
    $courseid=required_param('courseid',PARAM_INT);     // Course ID
    $sesskey=sesskey();     // session key
    $id=required_param('id',PARAM_INT);                 // Module ID

    $module->obtain_view_info($id,null,$cm,$course,$resourcepage);

    // Security checks
    $module->verify_edit_action($cm);


    $title = 'Adding shared page';

    // Show header
    $module->print_header($cm,$course,$resourcepage,$title,false);

    // get faculty details

    $category = '';
    $faculty = '';

    $sql = '';

    
    //$mr = new module_resourcepage();

    $mr = module_resourcepage::$inst;

    $sql1 = 'select a.name, a.path, b.category from prefix_course_categories a, prefix_course b where b.id= '.$courseid.' and a.id = b.category';


    $rs = $mr->do_sql($sql1);

    $category = $rs->fields['category'];
    $faculty = $rs->fields['name'];
    $categorypath = $rs->fields['path'];
    // $mr = new moodle_resourcepage();

    // get the shared course details to create the radio buttons

    $sql2 = "
SELECT 
    a.name, a.id AS resourcepageid, a.containerid, b.shortname, b.id, b.fullname 
FROM 
    prefix_resourcepage a 
    INNER JOIN prefix_course_modules cm ON a.id=cm.instance
    INNER JOIN prefix_modules m ON cm.module=m.id
    INNER JOIN prefix_course b ON cm.course=b.id
WHERE
    a.shared = 1
    AND a.id<>$resourcepage->id 
    AND b.category IN (SELECT id FROM prefix_course_categories cc where position(cc.path IN '$categorypath')=1)
    AND m.name='resourcepage'
";


    $rs = $mr->do_sql($sql2);

    

?>



    <form method="post" action="addshared.php" id="rp_addshared">

    <input type=hidden name="courseid" value='' />
    <input type=hidden name="containerid" value='' />

    <input type=hidden name="mode" value="edit"/>



<?php


    // pass across module id and sesskey


    printf("<input type=hidden name=\"id\" value='".$id."' />");

    
    printf("<input type=hidden name=\"sesskey\" value=\"".$sesskey."\" />");



    // print_heading_with_help($title, "mods", 'resourcepage_'.$extension);
    // print_simple_box_start('center', '', '', 5, 'generalbox', 'resourcepage_'.$extension);

    print_heading_with_help($title, "mods", null);                      // change mods to correct help file
    print_simple_box_start('center');

    

?>


        <h4>Shared pages for <?php print htmlspecialchars($faculty); ?>:</h4>



		<!-- 2nd inner table -->

		<table cellpadding='4'>


			<!-- titles -->



			<tr>
            <td></td>
            <th align='left'>Page</th>
			<th align='left' colspan='2'>Course</th>
			</tr>

			<!-- data -->


<?php
    


    $i = 0;

    while(!$rs->EOF) {

        // pick up and display the details

        $i++;

        // printf("<br>");

        $name = $rs->fields['name'];
        $coursecode = $rs->fields['shortname'];
        $fullname = $rs->fields['fullname'];
        $courseid = $rs->fields['id'];
        $resourcepageid = $rs->fields['resourcepageid'];
        $containerid = $rs->fields['containerid'];


        $sql3 = 'select id from prefix_course_modules where course = '.$courseid. ' and instance = '.$resourcepageid;


        $nrs = $mr->do_sql($sql3);


        $cmid = $nrs->fields['id'];


        // printf(" ".$name." ".$coursecode." ".$fullname." ".$id. " ".$resourcepageid." ".$containerid);

        print "
<tr>
<td align='left'>
<input type='radio' name='shareable' value='$resourcepageid,$courseid,$containerid' id='radio_shareable_$i' />
</td>
<td>
<label for='radio_shareable_$i'>$name</label> (<a href='view.php?id=$cmid'>link</a>)

</td>
<td>".htmlspecialchars($coursecode)."</td>
<td>".htmlspecialchars($fullname)."</td>
</tr>
";

        $rs->MoveNext();

    }


?>

</table>

<div style="margin-top:1em">
<input type="submit" value="Add shared resource" />
</div>


</form>

<?php



    // end outer table
  

    print_simple_box_end();


/*   

    // show page details

    printf("<br>");
    printf($sql);

    printf("<br>");
    printf($sql1);


    printf("<br>");
    printf($sql2);



    printf("<br>");
    printf($courseid);
    printf("<br>");
    printf($id);

    printf("<br>");
    printf($faculty);

**/



    // Main part ends
     
    print_footer($course);




 
?>